import java.util.Scanner;

public class Wait_Notify {

    private static Object locker=new Object();
    private static Object locker1=new Object();
    private static Object locker2=new Object();

    public static void main(String[] args) {
        Thread t1=new Thread(()->{
            System.out.println("A");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            synchronized (locker1){
                locker1.notify();//通知线程2
            }
        });
        Thread t2=new Thread(()->{
            synchronized (locker1){
                try {
                    locker1.wait(); //等待线程1通知
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("B");
            synchronized (locker2){
                locker2.notify();//通知线程3
            }
        });
        Thread t3=new Thread(()->{
            synchronized (locker2){
                try {
                    locker2.wait(); //等待线程2通知
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("C");
        });
        t1.start();
        t2.start();
        t3.start();
    }


    public static void main3(String[] args) {
        Thread t1=new Thread(()->{
            synchronized (locker){
                System.out.println("t1 wait之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1 wait之后");
            }
        });
        Thread t2=new Thread(()->{
            synchronized (locker){
                System.out.println("t2 wait之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t2 wait之后");
            }
        });
        Thread t3=new Thread(()->{
            synchronized (locker){
                System.out.println("t3 wait之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t3 wait之后");
            }
        });

        Thread t4=new Thread(()->{
            Scanner scanner=new Scanner(System.in);
            scanner.next();
            System.out.println("t4 notifyAll之前");
            synchronized (locker){
                locker.notifyAll();//随机唤醒上面wait的一个线程
            }
            System.out.println("t4 notifyAll之后");
        });
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

    public static void main2(String[] args) {
        Thread t1=new Thread(()->{
            synchronized (locker){
                System.out.println("t1 wait之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1 wait之后");
            }
        });

        Thread t2=new Thread(()->{
            System.out.println("t2 notify之前");
            Scanner scanner=new  Scanner(System.in);
            scanner.next();//构造一个阻塞情况
            synchronized (locker){
                locker.notify();//wait和notify要是同一个对象才能起作用
            }
            System.out.println("t2 notify之后");
        });
        t1.start();
        t2.start();
    }
    public static void main1(String[] args) throws InterruptedException {
        Object obj=new Object();
        System.out.println("wait之前");
        synchronized(obj) {
            obj.wait();

        }
        System.out.println("wait之后");
    }
}
